
import httpx
import requests
from my_paramiko import SSHConnection
import arrow
import sys
session = requests.session()


def main():
    print("hello world")
    res = session.get("https://192.168.8.12/login.html", verify=False)
    print(res.status_code)
    auth_data = {
        "user": "root",
        "password": "calvin"
    }
    res = session.post("https://192.168.8.12/data/login", data=auth_data)
    print(res.status_code)
    print(res.text)
    [print(n) for n in session.cookies.items]
    pass


def power_on():
    with SSHConnection("192.168.8.12", 22, "root", "calvin") as client:
        res = client.exec_command("racadm serveraction powerstatus")
        status = res.replace("Server power status: ", "").upper()
        print(res)
        if status == "OFF":
            res = client.exec_command("racadm serveraction powerup")
            print(res)
        client.exec_command("exit")
        client.close()


def power_off():
    with SSHConnection("192.168.8.12", 22, "root", "calvin") as client:
        res = client.exec_command("racadm serveraction powerstatus")
        status = res.replace("Server power status: ", "").upper()
        print(res)
        if status == "ON":
            res = client.exec_command("racadm serveraction powerdown")
            print(res)
        client.exec_command("exit")
        client.close()


def pve_shutdown():
    with SSHConnection("192.168.8.250", 22, "root", "xgxxgx") as client:
        client.exec_command("poweroff")


def check_server_status():
    with SSHConnection("192.168.8.12", 22, "root", "calvin") as client:
        res = client.exec_command("racadm serveraction powerstatus")
        status = res.replace("Server power status: ", "").upper()
        print(status)
        client.exec_command("exit")
        client.close()
        return status
    


if __name__ == "__main__":
    if len(sys.argv)>=2:
        arg=sys.argv[1]
        if str(arg).lower()=="poweron":
            print("命令行发现，应该开机")
            power_on()
            sys.exit()
        elif str(arg).lower()=="poweroff":
            print("命令行发现，应该关机")
            power_off()
            sys.exit()
        else:
            check_server_status()
    # main()
    # power_on()
    n = arrow.now("Asia/Shanghai")
    # print(n)
    # print(n.timestamp())
    # print(n.date())
    poweron_time1 = arrow.get(
        f'{n.date()} 09:00:00', 'YYYY-MM-DD HH:mm:ss', tzinfo="Asia/Shanghai")
    poweron_time2 = arrow.get(
        f'{n.date()} 19:30:00', 'YYYY-MM-DD HH:mm:ss', tzinfo="Asia/Shanghai")
    pveoff_time = arrow.get(f'{n.date()} 22:30:00',
                            'YYYY-MM-DD HH:mm:ss', tzinfo="Asia/Shanghai")
    poweroff_time = arrow.get(
        f'{n.date()} 23:30:00', 'YYYY-MM-DD HH:mm:ss', tzinfo="Asia/Shanghai")

    print(n.timestamp())
    print(poweron_time1.timestamp())
    print(pveoff_time.timestamp())
    print(poweroff_time.timestamp())
    print(n.timestamp() > poweron_time1.timestamp())

    if n.timestamp() > poweron_time1.timestamp() and n.timestamp() < poweron_time2.timestamp():
        print("应该开机")
       # power_on()
    elif n.timestamp() > pveoff_time.timestamp() and n.timestamp() < poweroff_time.timestamp():
        print("PVE关机")
        pve_shutdown()
    elif n.timestamp() > poweroff_time.timestamp():
        print("强制关机")
        power_off()
    else:
        print("检查系统状态")
        check_server_status()
